#####################################################
# survivor - written by chris beavers and tony daddeo
#
# tasks - make it so you can only ask about votes once or twice per sitting

#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dumper;
use List::Util 'shuffle';
use List::Util qw[min max];
use Term::ReadKey;
use Term::ANSIColor;
use Player;
use Time::Local;
use IO::Handle;

#####################
#### global vars ####

my $player;         # you
my $name;           # name of players
my $day;            # current day
my $hrs;            # hrs left before next event
my $tribe;          # tribe of players
my $merge_tribe = "default";
my $ready;
my $skill = "result";	    # current time stat
my $invent;         # inventory format
my $location;       # current location
my $idol_location;  # idol location
my $person;         # general player
my $complete;       # challenges
my $trib_current;
my $person_tribe;	# formatting
my $allies;			# allies printer helper
my @idol_sides;     # idol clues
my @map;            # map holder
my %words = ("survivor" => "./survivor_words.dat",
		"animals" => "./animal_words.dat",
		  "foods" => "./food_words.dat");	    # words dat files
my $TRIBE_SIZE = 10;
my $JURY_SIZE = 9;
my $MERGE_DAY = 19;
my @results;	    # [0] winner [1] loser
my %all;            # contains all current players
my %days;           # contains days text
my %ubungoo;        # list of players on ubungoo tribe
my %foodora;        # list of players on foodora tribe
my $voter;
my %votes;
my %jury;           # a hash from jury members to an array of people who voted
                    # them out
my %jury_alliances;  #
my $winner = "default";

my %item = ("mouse", 5, "coconut", 5,  "perch", 5,"berries", 10,
"bird", 15, "chicken", 15, "tilapia", 15, "crab", 25, "nuts", 25,
"marlin", 75, "chicken", 50, "tarp", 100, "rope", 50);

my %item_ally = ("mouse", 0, "bird", 0, "chicken", 3, "perch",0, "tilapia",
0, "crab", 2, "marlin", 3, "coconut", 0, "berries", 0,
"nuts", 1, "tarp", 5, "rope", 2, "hidden immunity idol", 9,
"immunity idol", 8);

my %commands = ("help", 1, "players", 1, "jury", 1, "foodora", 1, "talk", 1,
"fish", 1, "share", 1,
"ubungoo", 1, "eat", 1, "hunt", 1, "explore", 1, "idol", 1,
"move", 1, "mv", 1, "map", 1, "mytribe", 1, "advance", 1,
"today", 1, "gather", 1, "default", 1);

my @players = ("chris", "tony", "amanda", "james", "fairplay", "mikey",
"ozzy", "richard", "shambo", "yau man", "parvati", "tyson",
"coach", "russell",     "ethan", "amber", "tom", "vecepia",
"tina", "sandra", "natalie", "ken", "rupert", "jerri", "jt",
"sugar", "bob", "colby", "cirie", "courtney", "danielle");

my @positions = ("zeroth", "first", "second", "third", "fourth", "fifth",
"sixth", "seventh", "eighth", "ninth", "tenth", "eleventh",
"twelfth", "thirteenth", "fourteenth", "fifteenth",
"sixteenth", "seventeenth", "eighteenth");

################################## survivor ################################
# player gives name and is selected into tribe, next the player goes       #
# through each day, and has a challenge to gain stat points, get food, and #
# scheme. Then comes the immunity challenge followed by voting. repeat.    #
############################################################################

################################
######### load stuff ###########

open(DAYS_TEXT, "./days.dat") or die("Unable to open days");
my $count = 1;
$/ = "<BREAK>";
while(<DAYS_TEXT>)
{
    chomp;
    $days{$count} = $_;
    $count++;
}
$/ = "\n";
close(DAYS_TEXT);
###################
###### intro ######
srand();
system("clear");
print "what is your name?\n> ";
chomp($name = lc(<STDIN>));
while($name eq "" || $commands{$name})
{
    system('clear');
    print "please enter a valid name\n";
    chomp($name = lc(<STDIN>));
}
system('clear');
print "do you want to view the tutorial?\n> ";
chomp($_ = lc(<STDIN>));
if(/y./){&tutorial};
$player = new Player($name, 500, 500, 500, 10, 0, 0);
$player->addNullAlliance;
$tribe = &tribe_select;
&generate_map;
&generate_location($location);
&generate_location($idol_location);
&generate_idol_clue;
system("clear");    
print "welcome ", $name, " to surivor season 0x4D, on <ISLAND>!!1\n";
print "you are a member of the ", $tribe, " tribe.\n";
print "the rules are simple: do whatever you have to do to win.\n";
print "[type help at anytime for guidance]\n";
print "\n[press any key to continue]";
ReadMode('cbreak');
ReadKey(0);
ReadMode('normal');
my $start = 1;

###############
####  day  ####
$day = 1;
DAY: while($day < 39)
{
    if($start)
    {
        system('clear');
        print "day $day\n";
        print $days{$day};
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
        if(keys(%jury) eq $JURY_SIZE)
        {
            &jury_vote;
        }
        if($day == $MERGE_DAY)
        {
            $tribe = $name;
            system('clear');
            print "today the tribes merge!\n";
            while($commands{$merge_tribe})
            {
                print "what do you want to name the merged tribe?\n> ";
                chomp($merge_tribe = lc(<STDIN>));
                system('clear');
            }
            system('clear');
            print "welcome to the $merge_tribe tribe!\n";
            print "\n[press any key to continue]";
            ReadMode('cbreak');
            ReadKey(0);
            ReadMode('normal');
        }
		&adjust_stats;
		$trib_current = $tribe;
        $start = 0;
        $hrs = 8;
        $ready = 0;
    }
    system('clear');
    print "you have $hrs hours of daylight left, what would you";
    print " like to do?";
	if($day < $MERGE_DAY){
    	if($hrs < 1)
 		{print "\n[foodora  ubungoo talk  <player>  advance]";}
    	else
		{print "\n[foodora  ubungoo  talk   <player>  advance]\n[share    hunt     fish   gather    explore]";}
	}
	else
	{
    	if($hrs < 1)
 		{print "\n[players  jury  talk  <player>  advance]";}
    	else
		{print "\n[players  jury  talk   <player>  advance]\n[share    hunt     fish   gather    explore]";}
	}
    print "\n> ";
    chomp(my $input = lc(<STDIN>));
    system('clear');
    if(!&help_checker($input))
    {
        &command_checker($input, $hrs, $player);
    }
    if($ready)
    {
        $complete = 0;
        $day++;
        if($day % 2 == 0)
        {
            system('clear');
            print "and there goes your day. time for an immunity";
            print " challenge.\n";
            print "\n[press any key to continue]";
            ReadMode('cbreak');
            ReadKey(0);
            ReadMode('normal');
            goto CHALLENGE;
        }
        else
        {
            system('clear');
            print "the time has come for $results[1] to go to";
            print " tribal council.\n";
            print "\n[press any key to continue]";
            ReadMode('cbreak');
            ReadKey(0);
            ReadMode('normal');
             goto TRIBAL_COUNCIL;
           }
    }
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
}
CHALLENGE:    while(!$complete)
{
    if($winner ne "default")
    {
        $winner->loses("immunity idol", 1);
    }
	if($day % 10 == 0){@results = &merge_challenge_5($day/2);}
    elsif($day % 8 == 0){@results = &merge_challenge_4($day/2);}
    elsif($day % 6 == 0){@results = &merge_challenge_3($day/2);}
    elsif($day % 4 == 0){@results = &merge_challenge_1($day/2);}
    else{@results = &merge_challenge_2($day/2);}
    &adjust_rep(7);
    $complete = 1;
    $start = 1;
    $trib_current = $results[1];
    if($day/39 < rand(37)/39)
    { &add_votes($results[1], "weak"); }
    else
    { &add_votes($results[1], "strong"); }
    goto DAY;
}


$complete = 0;
TRIBAL_COUNCIL: while(!$complete)
{
    my $ply;
    if($tribe eq $results[1] || $tribe eq $name)
    {
        system('clear');
        print "welcome to the $positions[($day - 1)/2] tribal council.\n";
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
        $ply = &vote_my_tribe($results[1]);
    }
    else{
        $ply = &vote_their_tribe($results[1]);
    }
    system('clear');
    print "the $positions[($day - 1)/2] member voted off this season is $ply\n";
    print "*$ply, the tribal council has spoken*";
    print "\n[press any key to continue]";
    if($ply eq $player->getName)
    {
        goto END;
    }
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    $complete = 1;
    $start = 1;
    goto DAY;
}
my $end = 1;
END: while($end)
{
    print "\n[press any key to continue]";
    print "You lost\n";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    $end = 0;
    
}

###############################################
################# HELP CHECKER ################
sub help_checker
{
    my $full = $_[0];
    my @command = &get_commands($_[0]);
    my $command = $command[0];
    my @object = &get_objects($_[0]);
    my $object = $object[0];
    my $num = 1;
    if($_[0] =~ m/(\d+)/) {
        if($1 > 0)
        {
            $num = $1;
        }
    }
    if(@command > 1 || @object > 1)
    {
        print "please, one command at a time.\n";
        1;
    }
    elsif($person = $all{$_[0]})
    {
        $person = $all{$_[0]};
        $invent = &print_hash($person);
		if($foodora{$_[0]}){$person_tribe = "foodora";}
		else{$person_tribe = "ubungoo"};
        write;
        1;
    } 
    elsif(@command == 0)
    {
        0;
    }  
    elsif($command eq "help")
    {
        print "command      description                      hrs\n";           
        print '============ ================================ ===', "\n";
        print "map          view the island map              \n";
        print "move <direc> move <direc> on the map          \n";
        print "<player>     shows stats for a player         \n";
        if($tribe ne $name)
        {
            print "mytribe      show which tribe you are in      \n";
            print "ubungoo      show members of ubungoo tribe    \n";
            print "foodora      show members of foodora tribe    \n";
        }
        else
        {
            print "players      show everyone who\'s left        \n";
            print "jury         show the current jury members    \n";
        }
        print "advance      advance to next day              \n";
        print "talk         scheme with another player       \n";
        print "share        give something to your tribe     \n";
        print "eat          eat some food                    \n";
        print "hunt         hunt for animals                  1\n";
        print "fish         fish for fish (duh)               1\n";
        print "gather       gather fruits and vegetables      1\n";
        print "explore      who knows what you will find      2\n";
        1;
    }
    elsif($command eq "mytribe" && $tribe ne $name)
    {
        print "you are in the $tribe tribe.\nyou should probably keep";
        print " track of these things!\n";
        1;
    }
    elsif($command eq "ubungoo" && $tribe ne $name)
    {
        print "the members of ubungoo are:\n\n";
        $~ = "TEAM_TOP";
        write;
        $~ = "TEAM";
        foreach my $member (values(%ubungoo))
        {
            $person = $member;
            write;
        }
        $~ = "STDOUT";
        1;
    }
    elsif(($command eq "players" || $_[0] eq $tribe) && $tribe eq $name)
    {
        print "the remaining players are:\n\n";
        $~ = "TEAM_TOP";
        write;
        $~ = "TEAM";
        foreach my $member (values(%all))
        {
            $person = $member;
            write;
        }
        $~ = "STDOUT";
        1;
    }
    elsif($command eq "jury" && $tribe eq $name)
    {
        print "the jury members so far are:\n\n";
        foreach my $member (keys(%jury))
        {
            print "$member\n";
        }
        1;
    }
    elsif($command eq "map")
    {
        &print_map;
        1;
    }
    elsif($command eq "eat")
    {
        &eat($object, $num);
    }
    elsif($command eq "foodora" && $tribe ne $name)
    {
        print "the members of foodora are:\n\n";
        $~ = "TEAM_TOP";
        write;
        $~ = "TEAM";
        foreach my $member (values(%foodora))
        {
            $person = $member;
            write;
        }
        $~ = "STDOUT";
        1;
    }
    elsif($command eq "advance")
    {
        $ready = 1;
        1;
    }
    elsif($command eq "share")
    {
        &share($object, $num);
        1;
    }
    elsif($command eq "idol")
    {
        if($idol_location != -1)
        {
            my $last_side = pop(@idol_sides);
            foreach my $side (@idol_sides)
            {
                print $side . ", ";
            }
            if($last_side eq "water")
            {
                print "and $last_side surround the object preventing";
                print " your slaughter.\n";
            }
            elsif($last_side eq "land")
            {
                print "and $last_side encircle the item granting game\'s";
                print " command.\n";
            }
            else
            {
                print "and $last_side protect the idol of immunity.\n";
            }
            push(@idol_sides, $last_side);
        }
        else
        {
            print "you already have the hidden immunity idol!";
        }
    }
    elsif($command eq "talk")
    {
        my @schemers;
        foreach(keys %all)
        {
            if($_[0] =~ m/$_/i)
            {
                push(@schemers, $_);
            }
        }
        my @options = ("team up", "alliances", "votes", "trash", "end", "give");
        my @discuss;
        foreach(@options)
        {
            if($_[0] =~ m/$_/i)
            {
                push(@discuss, $_);
            }
        }
        my $discuss = $discuss[0];
        my $input = $schemers[0];
        if(!defined $input)
        {
            system('clear');
            print "who would you like to talk with?\n[$tribe]\n> ";
            chomp($input = lc(<STDIN>));
        }
        while(&talk($input, $discuss, $object, $num))
        {
            print "who would you like to talk with?\n[$tribe]\n> ";
            chomp($input = lc(<STDIN>));
            system('clear');
        }
        1;
    }
    elsif($full =~ m/mv/i || $full =~ m/move/i)
    {
        my @result;
        if($_[0] =~ m/east/i || $_[0] =~ m/ e/i)
        {
            @result = &move("east", $num);
            if($result[0])
            {
                print "you move east.\n";
            }
        }
        elsif($_[0] =~ m/west/i || $_[0] =~ m/ w/i)
        {
            @result = &move("west", $num);
            if($result[0])
            {
                print "you move west.\n";
            }
        }
        elsif($_[0] =~ m/north/i || $_[0] =~ m/ n/i)
        {
            @result = &move("north", $num);
            if($result[0])
            {
                print "you move north.\n";
            }
        }
        elsif($_[0] =~ m/south/i || $_[0] =~ m/ s/i)
        {
            @result = &move("south", $num);
            if($result[0])
            {
                print "you move south.\n";
            }
        }
        else
        {
            @result = (0);
        }
        if($result[0] eq 0)
        {
            print "you can\'t move that way, idiot.\n";
        }
        1;
    }
    else
    {
        0;
    }
}

sub get_commands
{
    my $input = $_[0];
    my @ret;    
    foreach(keys(%commands))
    {
        if($input =~ m/$_/i)
        {
            push(@ret, $_);
        }
    }
    return @ret;
}
sub get_objects
{
    my $input = $_[0];
    my @ret;    
    foreach(keys(%item_ally))
    {
        if($input =~ m/$_/i)
        {
            push(@ret, $_);
        }
    }
    return @ret;
}

#####################################################
################## COMMAND CHECKER ##################
sub command_checker
{
    my @ret;
    my $num;
    if($_[0] =~ m/(\d+)/) {
        if($1 > 0)
        {
            $num = $1;
        }
    }
    if(!defined $num)
    {
        $num = 1;
    }
    if($_[0] =~ m/hunt/i && $hrs >= $num)
    {
        for(my $i=0;$i<$num;$i++)
        {
            push(@ret, &hunt);
        }
    }
    elsif($_[0] =~ m/fish/i  && $hrs >= $num)
    {
        for(my $i=0;$i<$num;$i++)
        {
            push(@ret, &fish);
        }
    }
    elsif($_[0] =~ m/gather/i && $hrs >= $num) 
    {
        for(my $i=0;$i<$num;$i++)
        {
            push(@ret, &gather);
        }
    }
    if(@ret)
    {
        for(my $i = 0; $i<@ret;$i++)
        {
            if($i %2 == 0)
            {
                print "you found a ", $ret[$i], " worth ", $ret[$i+1],
        " strength points\n";
                if($ret[$i+1] != 0)
                {
                    print "this is now in your inventory\n";
                    $player->gets($ret[$i], 1);
                }
                $player->incStrength(-5);
            }
        }
        $hrs-=$num;
        undef @ret;
    }
    elsif($_[0] =~ m/explore/i && $hrs >= 2*$num)
    {
        for(my $i=0;$i<$num;$i++)
        {
            &explore;
            $player->incStrength(-10);
        }
        $hrs -= 2*$num;
    }
    else
    {
        print "sorry, that's not an option.\n";
    }
    
}


###################################################################
##################### command sub routines ########################

######## eat #######
sub eat
{ 
    my $input = $_[0];
    my $num = $_[1];
    if(!defined $input)
    {
        system('clear');
        print "what do you want to eat\?\n> ";
        chomp($input = lc(<STDIN>));
        if($input =~ m/(\d+)/) {
            if($1 > 0)
            {
                $num = $1;
            }
        }
        my @objects = &get_objects($input);
        if(@objects != 1)
        {
            print "just one food at a time please.\n";
            $input = "nothing";
        }
        else
        {
            $input = $objects[0];
        }
    }
    if(!defined $num)
    {
        $num = 1;
    }
    my %inventory = $player->gets;
    if($inventory{$input} >= $num && $item{$input} && $input ne "rope" && $input ne
    "tarp" && $input ne "hidden immunity idol" && $input ne "immunity idol")
    {
        system('clear');
        print "you eat $num $input.\n";
        print "your strength grows by ", $num * $item{$input}, "\n";
        $player->incStrength($num * $item{$input});
        $player->loses($input, $num);
    }
    elsif($input eq "nothing")
    {
        system('clear');
        print "you eat nothing.\n";
    }
    else
    {
        if($input eq "rope" || $input eq "tarp" || $input ne
            "hidden immunity idol" || $input ne "immunity idol")
        {
	    system('clear');
            print"thats not even possible\n";
        }
        else
        {
	    system('clear');
            print "i don\'t think that\'s yours to eat.\n";
        }
    }
}

####### SHARE #######
sub share
{
    my $input = $_[0];
    my $num = $_[1];
    if(!defined $input)
    {
        system('clear');
        print "what do you want to share\?\n> ";
        chomp($input = lc(<STDIN>));
        if($input =~ m/(\d+)/) {
            if($1 > 0)
            {
                $num = $1;
            }
        }
        my @objects = &get_objects($input);
        if(@objects != 1)
        {
            print "just one item at a time please.\n";
            $input = "nothing";
        }
        else
        {
            $input = $objects[0];
        }
    }
    if(!defined $num)
    {
        $num = 1;
    }
    my %inventory = $player->gets;
    if($inventory{$input} >= $num && $item{$input} && $input ne
    "hidden immunity idol" && $input ne "immunity idol")
    {
        system('clear');
        print "you give the group $num $input.\n";
        print "your reputation grows by ", $num * int($item{$input}), "\n";
        $player->incRep(int($num * $item{$input}));
        $player->loses($input, $num);
    }
    elsif($input eq "nothing")
    {
        system('clear');
        print "you give the group nothing.\n";
    }
    else
    {
	    system('clear');
        print "i don\'t think that\'s yours to give.\n";
    }
}

my %ha;
my @playas;
my @tricks;
my $asked;
my $asked_team;
####### TALK #######
sub talk
{
    system('clear');
    if($all{$_[0]})
    {
        if($_[0] eq $name)
        {
            print "get the straight jacket, you just talked to yourself.\n";
        }
        elsif(($tribe eq "foodora" && $ubungoo{$_[0]}) ||
        ($tribe eq "ubungoo" && $foodora{$_[0]}))
        {
            system('clear');
            print "that person\'s on the other tribe, dumdum!\n";
            return 1;
        }
        else
        {
            print "you are talking to $_[0].\n";
            &scheme($_[0], $_[1], $_[2], $_[3]);
            $asked = 0;
	        $asked_team = 0;
	        undef @tricks;
        }
        0;
    }    
    elsif(&help_checker($_[0]))
    {
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
        system('clear');
        1;
    }
    else
    {
        print "the heat must be getting to you... there's no player";
        print " with that name\n";
        1;
    }
}
sub scheme
{
    my $exit = 0;
    my $input = $_[1];
    my $object = $_[2];
    my $num = $_[3];
    if(!defined $input)
    {
        print "what do you want to talk to $_[0] about?\n";
        print "[help  give  votes  alliances  team up  trash  end]\n> ";
        chomp($input = lc(<STDIN>));
    }
    my $schemer = $_[0];
    
	#### set up lies
	%ha = &get_tribe($tribe);
    @playas = keys(%ha);
	@playas = shuffle(@playas);
    my $sure = 0;
    while($sure < @playas) 
    {
		if($playas[$sure] ne $schemer && !$all{$playas[$sure]}->isImmune) {push(@tricks,$playas[$sure])}
		$sure++;
	}
	####
    while(!&scheme_helper($input, $schemer))
    {
        print "what do you really want to talk to $_[0] about?\n";
        print "[type help for guidance]\n> ";
        chomp($input = lc(<STDIN>));
        system('clear');
    }
    system('clear');
    if($input eq "help")
    {
        print "command      description\n";
        print '============ =====================================', "\n";
        print "give         give an item to $schemer             \n";
        print "votes        discuss votes, upcoming and past     \n";
        print "alliances    find out who $schemer\'s tight with  \n";
        print "team up      join forces with $schemer            \n";
        print "trash        slander another player               \n";
        print "end          stop talking to $schemer             \n";
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
        system('clear');
    }
    elsif($input eq "give")
    {
        &give($schemer, $object, $num);
    }
    elsif($input eq "trash")
    {
        &trash($schemer);
    }
    elsif($input eq "team up" && $asked_team)
    {
	print "didn't i just give you an answer...?\n";
    }
    elsif($input eq "team up")
    {
        my $test = 0;
        foreach(&get_allies($schemer))
        {
            if($_ eq $name)
            {
                $test = 1;
            }
        }
        if($test)
        {
            system('clear');
            print "you\'re already allies with $schemer.\n";
        }
        elsif(($player->getRep/$all{$schemer}->getRep > .5 + rand(.6) || $player->getWit/$all{$schemer}->getWit > .5 + rand(.6)) && $all{$schemer}->getAlly/10 > rand(.8))
        {
            $player->joinAlliance($all{$schemer});
            $all{$schemer}->incAlly(1);
            system('clear');
            print "you team up with $schemer.\n";
        } 
        else
        {
            system('clear');
            print "$schemer isn\'t sure if you\'d make the best team.\n";
	    $asked_team = 1;
        }
    }
    elsif($input eq "votes" && $asked)
    {
	print "didn't i just tell you...?\n"
    }
    elsif($input eq "votes")
    {
	$asked = 1;
        my @votes = $all{$schemer}->getVotes;
	@votes = &remove_duplicates(@votes);
	if($all{$schemer}->getAlly < rand(7))
	{
      	        if(@votes eq 0)
        	{
        	    print "$schemer isn\'t sure who to vote for just yet.\n";
        	}
        	elsif (@votes == 1)
        	{
	
			  print "$tricks[0] is $schemer\'s first choice.\n";
			
        	}
        	elsif (@votes == 2)
        	{
	
   				print "$tricks[0] and $tricks[1] are $schemer\'s current";
        			print " top options.\n";
        	}
        	else
        	{
   			  print "$tricks[0], $tricks[1] ";
        	  	  print "and $tricks[2] are all on $schemer\'s";
        	   	  print " chopping block.\n";
        	}
	  }
	  else
	  {
        	if(@votes eq 0)
        	{
        	    print "$schemer isn\'t sure who to vote for just yet.\n";
        	}
        	elsif (@votes == 1)
        	{
	
			  print "$votes[0] is $schemer\'s first choice.\n";
			
        	}
        	elsif (@votes == 2)
        	{
	
   				print "$votes[0] and $votes[1] are $schemer\'s current";
        			print " top options.\n";
        	}
        	else
        	{
   			  print "$votes[0], $votes[1] ";
        	  	  print "and $votes[2] are all on $schemer\'s";
        	   	  print " chopping block.\n";
        	}
	   }
    }
    elsif($input eq "alliances")
    {
        my @buddies = &get_allies($schemer);
        if(@buddies == 0)
        {
            print "$schemer has no alliances.\n";
        }
        elsif (@buddies == 1)
        {
            print "$buddies[0] is $schemer\'s only ally.\n";
        }
        elsif (@buddies == 2)
        {
            print "$buddies[0] and $buddies[1] are $schemer\'s allies.\n";
        }
        else
        {
            my $last_buddy = pop(@buddies);
            foreach(@buddies)
            {
                print "$_, ";
            }
            print "and $last_buddy are all $schemer\'s allies.\n";
        }
    }
    elsif($input eq "end")
    {
        $exit = 1;
    }
    if($exit == 1)
    {
        system('clear');
        print "you and $schemer end your discussion before anyone takes";
        print " too much notice.\n";
    }
    else
    {
        &scheme($schemer);
    }
}
sub trash
{
    my $schemer = $_[0];
    system('clear');
    print "who do you want to slander to $schemer\?\n> ";
    chomp(my $input = lc(<STDIN>));
    if($all{$input})
    {
        system('clear');
        if($input eq $schemer)
        {
            print "you ramble on about what an awful person $input is right to their face.\n";
            print "i think it\'s safe to say they hate you now.\n";
            $all{$input}->incAlly(-3);
            for(my $i = 0; $i < int(rand(5)); $i++)
            {
                $all{$schemer}->addVote($name);
            }
        }
        else {
            print "you tear up $input\'s good name.\n";
            print "be careful though; slander them too much and they\'ll start";
            print " getting pitty.\n";
            print "and you\'ll just look like a jerk.\n\n";
            $all{$input}->incRep(int(-rand($all{$input}->getRep/4 - 75)));
            $player->incRep(int(-rand(50)));
            $all{$input}->incAlly(-1);
			my $count = int(rand($all{$schemer}->getAlly /2));
            for(my $i = 0; $i < $count; $i++)
            {
                $all{$schemer}->addVote($input);
            }
            print "$schemer will consider voting out $input.\n";
        }
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
        system('clear');
    }
    elsif($input eq "ubungoo")
    {
        system('clear');
        print "the members of ubungoo are:\n\n";
        $~ = "TEAM_TOP";
        write;
        $~ = "TEAM";
        foreach my $member (values(%ubungoo))
        {
            $person = $member;
            write;
        }
        $~ = "STDOUT";
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
        system('clear');
        &trash($schemer);                
    }
    elsif($input eq "foodora")
    {
        system('clear');
        print "the members of foodora are:\n\n";
        $~ = "TEAM_TOP";
        write;
        $~ = "TEAM";
        foreach my $member (values(%foodora))
        {
            $person = $member;
            write;
        }
        $~ = "STDOUT";
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
        system('clear');
        &trash($schemer);
    }
    else
    {
        system('clear');
        print "next time you slander something, at";
        print " least make it a fellow player.\n";
    }
}
sub get_allies
{
    my $schemer = $_[0];
	my $tribe_here = $trib_current;
    my @buddies;
    my %tribe;
    if($tribe_here eq "foodora")
    {
        %tribe = %foodora;
    }
    elsif($tribe_here eq "ubungoo")
    {
        %tribe = %ubungoo;
    }
    else 
    {
        %tribe = %all;
    }
    foreach( values(%tribe) )
    {
        foreach my $alliance (@{$_->getAlliance})
        {
            if(&contains($alliance, @{$all{$schemer}->getAlliance}))
            {
                push(@buddies, $_->getName);
            }
        }
    }
    return &remove_duplicates(@buddies);
}

sub give
{
    my $schemer = $_[0];
    my $input = $_[1];
    my $num = $_[2];
    if(!defined $input)
    {
        system('clear');
        print "what do you want to give $schemer\?\n> ";
        chomp($input = lc(<STDIN>));
        if($input =~ m/(\d+)/) {
            if($1 > 0)
            {
                $num = $1;
            }
        }
        my @obj = &get_objects($input);
        if(@obj != 1)
        {
            print "one type of object at a time please.\n";
            $input = "nonsense";
        }
        else {
            $input = $obj[0];
        }
    }
    if(!defined $num)
    {
        $num = 1;
    }
    my %inventory = $player->gets;
    if($inventory{$input} >= $num)
    {
        system('clear');
        print "you give $schemer $num $input.\n";
        $all{$schemer}->incAlly($num * $item_ally{$input});
        $player->loses($input, $num);
        $all{$schemer}->gets($input, $num);
    }
    else
    {
        system('clear');
        print "i don\'t think that\'s yours to give.\n";
    }
}

sub tribe
{
    if($tribe eq "foodora")
    {
        return %foodora;
    }
    elsif($tribe eq "ubungoo")
    {
        return %ubungoo;
    }
    else
    {
        return %all;
    }
}

sub scheme_helper
{
    if($_[0] eq "give" || $_[0] eq "end" ||  $_[0] eq "votes"
        || $_[0] eq "alliances" || $_[0] eq "help" || $_[0] eq "trash"
        || $_[0] eq "team up")
    {
        1;
    }
    else
    {
        system('clear');
        print "why would you possibly want to";
        print " talk to $_[1] about $_[0]?!\n";
        0;
    }
}

####### HUNT #########
sub hunt
{
    my $prob = rand();
    if($prob < .5)
    {            
        ("mouse", 5);
    }
    elsif($prob < .7)
    {            
        ("bird", 15);
        
    }
    elsif($prob < .8)
    {
        ("chicken", 50);
    }
    else
    {
        ("\bbsolutely nothing", 0);
    }
}

######## FISH ########
sub fish
{
    my $prob = rand();
    if($prob < .35)
    {
        ("perch", 5);
    }
    elsif($prob < .5)
    {
        ("tilapia", 15);
        
    }
    elsif($prob < .62)
    {
        ("crab", 25);
    }
    elsif($prob < .7)
    {
        ("marlin", 75);
    }
    else
    {
        ("\bbsolutely nothing", 0);
    }
}

######## GATHER #########
sub gather
{
    my $prob =  rand();
    if($prob < .60)
    {
        ("coconut", 5);
    }
    elsif($prob < .75)
    {
        ("berries", 10);
    }
    elsif($prob < .95)
    {
        ("nuts", 25);
    }
    else
    {
        ("\bbsolutely nothing", 0);
    }
}

###### explore ######
sub explore
{
    if(rand() < $player->{wit}/($player->{wit} + 50) &&
    $location == $idol_location)
    {
        print "you found the hidden immunity idol!!\n";
        print "this is now in your inventory\n";
        $player->gets("hidden immunity idol", 1);
        $idol_location = -1;
        undef @idol_sides;
    }
    elsif(rand() < $player->{wit}/($player->{wit} + 3750))
    {
        print "you found a rope and gain 25 wit points\n";
        print "this is now in your inventory\n";
        $player->gets("rope", 1);
        $player->incWit(25);
    }
    elsif(rand() < $player->{wit}/($player->{wit} + 7500))
    {
        print "you found a tarp and gain 50 wit points\n";
        print "this is now in your inventory\n";
        $player->gets("tarp", 1);
        $player->incWit(50);
    }
    elsif(rand() < $player->{wit}/($player->{wit} + 2500) &&
    $map[$location] eq "###")
    {
        my ($item, $nop) = &hunt;
        if($nop != 0)
        {
            print "you found a $item worth $nop strength points";
            print " and gained 5 wits point\n";
            print "this is now in your inventory\n";
            $player->gets("$item", 1);
            $player->incWit(5);
        }
        else
        {
            print "no luck today.\n"
        }
    }
    elsif(rand() < $player->{wit}/($player->{wit} + 2250) &&
    ($map[$location] eq "###" || $map[$location] eq "^@^"))
    {
        my ($item, $nop) = &gather;
        if($nop != 0)
        {
            print "you found a $item worth $nop strength points";
            print " and gained 5 wit point\n";
            print "this is now in your inventory\n";
            $player->gets("$item", 1);
            $player->incWit(5);
        }
        else
        {
            print "no luck today.\n"
        }
    }
    elsif(rand() < $player->{wit}/($player->{wit} + 1800) &&
    $map[$location] eq "~~~")
    {
        my ($item, $nop) = &fish;
        if($nop != 0)
        {
            print "you found a $item worth $nop strength";
            print " points and gained 5 wit point\n";
            print "this is now in your inventory\n";
            $player->gets("$item", 1);
            $player->incWit(5);
        }
        else
        {
            print "no luck today.\n"
        }
    }
    else
    {
        print "no luck today.\n"
    }
}

#########################################
########### tribe selector ##############

sub tribe_select 
{
    my @newplayers = shuffle(@players);
    undef @players; 
    foreach my $playa (@newplayers)
    {
        unless($playa eq $name)
        {
            my $temp = new Player($playa, int(250 + rand(500)),
            int(250 + rand(500)), 490 + int(rand(20)), 5, 0, 0);
            push(@players, $temp);
        }
    }
    
    my $tribe_num = rand();
    if($tribe_num < .5)
    {
        $ubungoo{$name} = $player;
        $all{$name} = $player;
        my $i;
        for($i=0; $i < $TRIBE_SIZE - 1; $i++)
        {
            my $temp = pop(@players);
            $all{$temp->getName} = $temp;
            $ubungoo{$temp->getName} = $temp;
        }
        for($i=0; $i < $TRIBE_SIZE; $i++)
        {
            my $temp = pop(@players);
            $all{$temp->getName} = $temp;
            $foodora{$temp->getName} = $temp;
        }
        return "ubungoo";    
    } else 
    {
        $foodora{$name} = $player;
        $all{$name} = $player;
        my $i;
        for($i=0; $i < $TRIBE_SIZE - 1; $i++)
        {
            my $temp = pop(@players);
            $all{$temp->getName} = $temp;
            $foodora{$temp->getName} = $temp;
        }
        for($i=0; $i < $TRIBE_SIZE; $i++)
        {
            my $temp = pop(@players);
            $all{$temp->getName} = $temp;
            $ubungoo{$temp->getName} = $temp;
        }
        return "foodora";
    }
}

######################################################
################### CHALLENGE SUBS ###################

########## immunity challenge 1 ##########
sub merge_challenge_1
{
    my @w = keys(%words);
    my $category = $w[int(rand(@w))];
    my $file = $words{$category};
	$skill = "time";
    system('clear');
    print "welcome to challenge number ", $_[0];
    print "\ngame: scrambler";
    print "\nobjective: unscramble the words as fast as possible";
    print "\nword category: $category\n";
    print "\n[press any key to begin]";
    my $time = &scrambler($file);
    print "your total time was $time seconds";
    $person = &getNsetTimes("wit",$time,8);
    $player->setTime($time);
    print "\nthe best time was ", $person->getTime,
    " by ",$person->getName;
    my @ret;
    if($tribe ne $name)
    {
        my $first_sum = 0;
        my $second_sum = 0;
        foreach my $temp (values(%foodora))
        {
            $first_sum += $temp->getTime;
        }
        foreach my $temp (values(%ubungoo))
        {
            $second_sum += $temp->getTime;
        }
        if ($first_sum/values(%foodora) - 2*values(%foodora) < $second_sum/values(%ubungoo) - 2*values(%ubungoo))
        {
            @ret = ("foodora", "ubungoo");
        }
        else
        {
            @ret = ("ubungoo", "foodora");
        }
        if($ret[0] eq $tribe)
        {
            print "\nyour tribe won immunity!\n";
        }
    }
    else
    {
        $winner = $person;
		$winner->incRep(15);
        my $winnerName = $winner->getName;
	$person->gets("immunity idol", 1);
        print "\n$winnerName won the immunity idol!";
        print "\nthis is now in $winnerName\'s inventory.\n";
        print "you cannot vote out $winnerName tonight.\n";
        @ret = ($winnerName, "others");
    }
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    print "the winner of the challenge is $ret[0]\!";
    print "\n$ret[1] you have a date with me at tribal council.\n";
    print "$ret[0] enjoy the day off.\n";
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    return @ret;
}
## it would be nice to make it so there is just challenge($challenge_number), since most of this is repeating.
## we can handle printing individual stuff with hashes maybs
sub merge_challenge_2
{
    my @w = keys(%words);
    my $category = $w[int(rand(@w))];
    my $file = $words{$category};
    $skill = "correct";
    system('clear');
    print "welcome to challenge number ", $_[0];
    print "\ngame: memory";
    print "\nobjective: remember the words";
    print "\nword category: $category\n";
    print "\n[press any key to begin]";
    my $time = &memory($file);
    system('clear');
    print "your total number of words was $time";
    $person = &getNsetMax("wit",$time,8);
    $player->setTime($time);
    print "\nthe most number of words was ", $person->getTime,
    " by ",$person->getName;
    my @ret;
    if($tribe ne $name)
    {
        my $first_sum = 0;
        my $second_sum = 0;
        foreach my $temp (values(%foodora))
        {
            $first_sum += $temp->getTime;
        }
        foreach my $temp (values(%ubungoo))
        {
            $second_sum += $temp->getTime;
        }
        if ($first_sum/values(%foodora) + values(%ubungoo) - values(%foodora) > $second_sum/values(%ubungoo) + values(%foodora) - values(%ubungoo))
        {
            @ret = ("foodora", "ubungoo");
        }
        else
        {
            @ret = ("ubungoo", "foodora");
        }
        if($ret[0] eq $tribe)
        {
            print "\nyour tribe won immunity!\n";
        }
    }
    else
    {
        $winner = $person;
		$winner->incRep(15);
        my $winnerName = $winner->getName;
	$person->gets("immunity idol", 1);
        print "\n$winnerName won the immunity idol!";
        print "\nthis is now in $winnerName\'s inventory.\n";
        print "you cannot vote out $winnerName tonight.\n";
        @ret = ($winnerName, "others");
    }
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    print "the winner of the challenge is $ret[0]\!";
    print "\n$ret[1] you have a date with me at tribal council.\n";
    print "$ret[0] enjoy the day off.\n";
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    return @ret;
}


## it would be nice to make it so there is just challenge($challenge_number), since most of this is repeating.
## we can handle printing individual stuff with hashes maybs
sub merge_challenge_3
{
    $skill = "words";
    system('clear');
    print "welcome to challenge number ", $_[0];
    print "\ngame: speed typing";
    print "\nobjective: type the words as fast as possible";
    print "\nword category: all\n";
    print "\n[press any key to begin]";
    my $time = &speed;
    system('clear');
    print "your total number of words was $time";
    $person = &getNsetMax("wit",$time,9);
    $player->setTime($time);
    print "\nthe most number of words was ", $person->getTime,
    " by ",$person->getName;
    my @ret;
    if($tribe ne $name)
    {
        my $first_sum = 0;
        my $second_sum = 0;
        foreach my $temp (values(%foodora))
        {
            $first_sum += $temp->getTime;
        }
        foreach my $temp (values(%ubungoo))
        {
            $second_sum += $temp->getTime;
        }
        if ($first_sum/values(%foodora) + values(%ubungoo) - values(%foodora) > $second_sum/values(%ubungoo) + values(%foodora) - values(%ubungoo))
        {
            @ret = ("foodora", "ubungoo");
        }
        else
        {
            @ret = ("ubungoo", "foodora");
        }
        if($ret[0] eq $tribe)
        {
            print "\nyour tribe won immunity!\n";
        }
    }
    else
    {
        $winner = $person;
		$winner->incRep(15);
        my $winnerName = $winner->getName;
	$person->gets("immunity idol", 1);
        print "\n$winnerName won the immunity idol!";
        print "\nthis is now in $winnerName\'s inventory.\n";
        print "you cannot vote out $winnerName tonight.\n";
        @ret = ($winnerName, "others");
    }
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    print "the winner of the challenge is $ret[0]\!";
    print "\n$ret[1] you have a date with me at tribal council.\n";
    print "$ret[0] enjoy the day off.\n";
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    return @ret;
}

sub merge_challenge_5
{
    my @w = keys(%words);
    my $category = $w[int(rand(@w))];
    my $file = $words{$category};
    system('clear');
    print "game: colors";
    print "\nobjective: type the either the text or the color of the text";
    print "\nword category: colors";
    print "\n[press any key to begin]";
    my $time = &colors();
    print "your total time was $time seconds";
    $person = &getNsetTimes("wit",$time,9);
    $player->setTime($time);
    print "\nthe best time was ", $person->getTime,
    " by ",$person->getName;
    my @ret;
    if($tribe ne $name)
    {
        my $first_sum = 0;
        my $second_sum = 0;
        foreach my $temp (values(%foodora))
        {
            $first_sum += $temp->getTime;
        }
        foreach my $temp (values(%ubungoo))
        {
            $second_sum += $temp->getTime;
        }
        if ($first_sum/values(%foodora) - 2*values(%foodora) < $second_sum/values(%ubungoo) - 2*values(%ubungoo))
        {
            @ret = ("foodora", "ubungoo");
        }
        else
        {
            @ret = ("ubungoo", "foodora");
        }
        if($ret[0] eq $tribe)
        {
            print "\nyour tribe won immunity!\n";
        }
    }
    else
    {
        $winner = $person;
		$winner->incRep(15);
        my $winnerName = $winner->getName;
	$person->gets("immunity idol", 1);
        print "\n$winnerName won the immunity idol!";
        print "\nthis is now in $winnerName\'s inventory.\n";
        print "you cannot vote out $winnerName tonight.\n";
        @ret = ($winnerName, "others");
    }
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    print "the winner of the challenge is $ret[0]\!";
    print "\n$ret[1] you have a date with me at tribal council.\n";
    print "$ret[0] enjoy the day off.\n";
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    return @ret;
}


sub merge_challenge_4
{
	$skill = "time";
    system('clear');
    print "welcome to challenge number ", $_[0];
    print "\ngame: speed math";
    print "\nobjective: do the math as fast as possible";
    print "\n[press any key to begin]";
    my $time = &speedmath();
    print "your total time was $time seconds";
    $person = &getNsetTimes("wit",$time,9);
    $player->setTime($time);
    print "\nthe best time was ", $person->getTime,
    " by ",$person->getName;
    my @ret;
    if($tribe ne $name)
    {
        my $first_sum = 0;
        my $second_sum = 0;
        foreach my $temp (values(%foodora))
        {
            $first_sum += $temp->getTime;
        }
        foreach my $temp (values(%ubungoo))
        {
            $second_sum += $temp->getTime;
        }
        if ($first_sum/values(%foodora) - 2*values(%foodora) < $second_sum/values(%ubungoo) - 2*values(%ubungoo))
        {
            @ret = ("foodora", "ubungoo");
        }
        else
        {
            @ret = ("ubungoo", "foodora");
        }
        if($ret[0] eq $tribe)
        {
            print "\nyour tribe won immunity!\n";
        }
    }
    else
    {
        $winner = $person;
		$winner->incRep(15);
        my $winnerName = $winner->getName;
	$person->gets("immunity idol", 1);
        print "\n$winnerName won the immunity idol!";
        print "\nthis is now in $winnerName\'s inventory.\n";
        print "you cannot vote out $winnerName tonight.\n";
        @ret = ($winnerName, "others");
    }
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    print "the winner of the challenge is $ret[0]\!";
    print "\n$ret[1] you have a date with me at tribal council.\n";
    print "$ret[0] enjoy the day off.\n";
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    return @ret;
}

sub scrambler
{   
    open(FILE, $_[0]) || die("unable to open words file");
    my @words;
    my $time;
    while(<FILE>)
    {
        chomp;
        push(@words, $_);
    }
    close(FILE);
    @words = shuffle(@words);
    my @challenge_words;
    push(@challenge_words, pop(@words));
    push(@challenge_words, pop(@words));
    push(@challenge_words, pop(@words));
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    foreach my $word (@challenge_words)
    {
        my $time_start = time();
        my $wordcopy = $word;
        my @chars = split(//,$word);
        @chars = shuffle(@chars);
	    my $input = "neverever";
        while($input ne $word)
        {
            system('clear');
	        print "[or shuffle]\n";
            print "word: @chars";
            print "\nguess: ";
            chomp($input = <STDIN>);
	        if($input ne $word)
		        {@chars = shuffle(@chars);}
        }
        
        my $time_ellapsed = time() - $time_start;
        $time += $time_ellapsed;
        system('clear');
        print "time ellapsed: $time_ellapsed\n";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
    }
    $time;
}
my @challenge_words;
sub memory
{    
    open(FILE, $_[0]) || die("unable to open words file");
    my @words;
    my $time;
    while(<FILE>)
    {
        chomp;
        push(@words, $_);
    }
    close(FILE);
    @words = shuffle(@words);
    for(my $j = 0; $j <16; $j++)
    {$challenge_words[$j] = pop(@words);}
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    $~ = "MEMORY";
    write;
    $~ = "STDOUT";
    ReadMode('noecho');
    ReadKey(10);
    ReadMode('normal');
    system('clear');
	my $guesses = 20;
	my $correct = 0;
	my @right;
		$_ = undef;
       		while($guesses-- > 0)
		{
	  	system('clear');
	  	print "guesses left: ",$guesses + 1;
        	  print "\ncorrect so far: $correct\n";
		  print "guess: ";
		  chomp(my $word = <STDIN>);
		  if(&contains($word, @challenge_words) && !&contains($word, @right))
		   {$correct++; push(@right, $word);}
		}
    $correct;
}
sub speed
{  
    my @arr;
    my $time;
    foreach my $file (values(%words))
    {
    	open(FILE, $file) || die("unable to open words file");
    	while(<FILE>)
    	{
        	chomp;
        	push(@arr, $_);
    	}
    }
    close(FILE);
    @arr = shuffle(@arr);
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    my $time_start = time();
    my $time_ellapsed = 0;
    my $correct = 0;
    while($time_ellapsed < 25)
    {
	$time_ellapsed = time() - $time_start;
	my $input = "caca";
	my $word = pop(@arr);
       	while($input ne $word && $time_ellapsed < 25)
	{
	  system('clear');
	  print "correct: ", $correct;
	  print "\nword: ", $word;
	  print "\n> ";
	  chomp($input = lc(<STDIN>));
	}
	if($word eq $input){$correct++;}
    }
    $correct;
}

sub colors
{  
my $time = 0;
my @colors = ("white", "red", "green", "yellow", "blue", "cyan", "magenta");
my @fore = qw(white red green yellow blue magenta cyan);
	@colors = shuffle(@colors);
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    foreach my $word (@colors)
	{
		my $correct = 0;
		my $type;
        my $time_start = time();
	    if(rand() < .5){$type = "color";}
	    else {$type = "word";}
        my $color = @fore[int(rand(@fore))];
        if($type eq "color"){
           while(!$correct)
           {
            system('clear');
			print "[", color("white"), "white ", color("red"), "red ", color("green"), "green ", color("yellow"), "yellow ", color("blue"), "blue ", color("magenta"), "magenta ", color("cyan"), "cyan", color("reset"), "]\n";
            print "word: ";
			print color("$color on_black"), "$word\n";
			print color("reset"), "color: ";
            chomp(my $input = <STDIN>);
	        if($input eq $color)
		        {$correct = 1;}
            }
	    }
        else{
            while(!$correct)
            {
            system('clear');
			print "[", color("white"), "white ", color("red"), "red ", color("green"), "green ", color("yellow"), "yellow ", color("blue"), "blue ", color("magenta"), "magenta ", color("cyan"), "cyan", color("reset"), "]\n";
            print "word: ";
			print color("$color on_black"), "$word\n";
			print color("reset"), "text: ";
            chomp(my $input = <STDIN>);
	        if($input eq $word)
		        {$correct = 1;}
            }
	    }
        my $time_ellapsed = time() - $time_start;
        $time += $time_ellapsed;
        print color("reset");
        system('clear');
        print "time ellapsed: $time_ellapsed\n";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
    }
    $time;
}
sub speedmath
{   
    my $time;
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    for(my $i = 0; $i < 10; $i++)
    {
        my $num1 = int(rand(12)) + 1;
        my $num2 = int(rand(12)) + 1;
        my $oper = int(rand(3));
        my $oper_str;
        my $answer;
        if($oper == 0)
        {
            $answer = $num1 + $num2;
            $oper_str = "+";
        }
        elsif($oper == 1)
        {
            $answer = $num1 - $num2;
            $oper_str = "-";
        }
        else
        {
            $answer = $num1 * $num2;
            $oper_str = "*";
        }
        my $time_start = time();
	    my $input = "neverever";
        while($input ne $answer)
        {
            system('clear');
            print "$num1 $oper_str $num2 = ";
            chomp($input = <STDIN>);
        }
        my $time_ellapsed = time() - $time_start;
        $time += $time_ellapsed;
        system('clear');
        print "time ellapsed: $time_ellapsed\n";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
    }
    $time;
}

##############################################
############### MAP FUNCTIONS ################
sub generate_map
{
    for(my $i=0; $i < 100; $i++)
    {
        my $rand = rand();
        if($i==0||$i==1||$i==8||$i==9||$i==10||$i==19||$i==80||$i==89
            ||$i==90||$i==91||$i==98||$i==99)
        {
            $map[$i] = "~~~";
        }
        elsif($i < 10 || $i > 90 || ($i+1) % 10 == 0 || ($i) % 10 == 0)
        {
            &map_gen_helper($rand, .3, $i);
        }
        elsif($i < 20 || $i > 80 || ($i+2) % 10 == 0 || ($i-1) % 10 == 0)
        {
            &map_gen_helper($rand, .6, $i);
        }
        elsif($i < 30 || $i > 70 || ($i+3) % 10 == 0 || ($i-2) % 10 == 0)
        {
            &map_gen_helper($rand, .85, $i);
        }
        else
        {
            &map_gen_helper($rand, .98, $i);
        }
    }
}

sub map_gen_helper
{
    if($_[0] < $_[1])
    {
        my $rand2 = rand();
        if($rand2 < .8)
        {
            $map[$_[2]] = "###";
        }
        else
        {
            $map[$_[2]] = "^@^";
        }
    }
    else
    {
        $map[$_[2]] = "~~~";
    }
}

sub move
{
    if($_[0] eq "west") # on left boundary
    {
        my $max = 0;
        if(($location + 1) % 10 == 0)
        {
            $max++;
        }
        while(($location + 1 - $max) % 10 != 0)
        {
            $max++;
        }
        if($_[1] >= $max)
        {
            (0, 0);
        }
        else
        {
            $location-= $_[1];
            &move_helper;
        }
    }
    elsif($_[0] eq "east") # on right boundary
    {
        my $max = 0;
        if($location % 10 == 0)
        {
            $max++;
        }
        while(($location + $max) % 10 != 0)
        {
            $max++;
        }
        if($_[1] >= $max)
        {
            (0, 0);
        }
        else
        {
            $location+= $_[1];
            &move_helper;
        }
    }
    elsif($_[0] eq "north")
    {
        if($location <= 9 * $_[1])
        {
            (0, 0);
        }
        else
        {
            $location -= (10 * $_[1]);
            &move_helper;
        }
    }
    elsif($_[0] eq "south")
    {
        if($location + (10 * ($_[1] - 1)) >= 90)
        {
            (0, 0);
        }
        else
        {
            $location += (10 * $_[1]);
            &move_helper;
        }
    }
}

sub generate_location
{
    $_[0] = 1;
    while($map[$_[0]] eq "~~~")
    {
        $_[0] = int(rand(100));
    }
}

sub move_helper
{
    if($map[$location] eq "###")
    {
        (1, 0);
    }
    elsif($map[$location] eq "~~~")
    {
        (1, 1);
    }
    else
    {
        (1, 2);
    }
}

sub print_map
{
    print "MAP OF <ISLAND>:\n\n";
    for(my $i=0; $i < 100; $i++)
    {
        print "|";
        
        if($i == $location)
        {
            if($map[$i] eq "~~~")
            {
                print color("black on_blue");
                print "~!~";
            }
            elsif($map[$i] eq "###")
            {
                print color("black on_green");
                print "#!#";
            }
            else
            {
                print color("black on_green");
                print "^!^";
            }
            print color("reset");
        }
        else
        {
            if($map[$i] eq "~~~")
            {
                print color("white on_blue");
                print "~~~";
            }
            elsif($map[$i] eq "###")
            {
                print color("yellow on_green");
                print "###";
            }
            else
            {
                print color("yellow on_green");
                print "^@^";
            }
            print color("reset");
        }
        
        if(($i+1) % 10 == 0)
        {
            print "|";
            if($i == 9)
            {
                print "   ", color("white on_blue"), "~~~", color("reset"), " - water";
            }
            elsif($i == 19)
            {
                print "   ", color("yellow on_green"), "###", color("reset"), " - land";
            }
            elsif($i == 29)
            {
                print "   ", color("yellow on_green"), "^@^", color("reset"), " - tree";
            }
            elsif($i == 39)
            {
                print "   ", color("black on_cyan"), " ! ", color("reset"), " - you are here";
            }
            elsif($i == 69)
            {
                print "       ", color("red on_black"), "N";
            }
            elsif($i == 79)
            {
                print "     ", color("red on_black"), "W x E";
            }
            elsif($i == 89)
            {
                print "       ", color("red on_black"), "S";
            }
            print color("reset");
            print "\n";
        }
    }
}

sub generate_idol_clue
{
    if($idol_location % 10 != 0)
    {
        &add_idol_side(1);
    }
    if(($idol_location + 1) % 10 != 0)
    {
        &add_idol_side(-1);
    }
    if($idol_location > 9)
    {
        &add_idol_side(-10);
    }
    if($idol_location < 90)
    {
        &add_idol_side(10);
    }
}

sub add_idol_side
{
    if($map[$idol_location + $_[0]] eq "~~~")
    {
        push(@idol_sides, "water");
    }
    elsif($map[$idol_location + $_[0]] eq "###")
    {
        push(@idol_sides, "land");
    }
    else
    {
        push(@idol_sides, "tree");
    }
}

######################
#### voting subs #####

sub vote_my_tribe
{
    my $tribe_ref;
    if($_[0] eq "ubungoo")
    {
        $tribe_ref = \%ubungoo;
    }
    elsif($_[0] eq "foodora")
    {
        $tribe_ref = \%foodora;
    }
    else
    {
        $tribe_ref = \%all;
    }
    my $input = "bsada";
    while(!$tribe_ref->{$input} || $tribe_ref->{$input}->isImmune)
    {
        system('clear');
	if($input eq "ubungoo" && $tribe ne $name)
   	{
        print "the members of ubungoo are:\n\n";
        $~ = "TEAM_TOP";
        write;
        $~ = "TEAM";
        foreach my $member (values(%ubungoo))
        {
            $person = $member;
            write;
        }
        $~ = "STDOUT";
	print "\n[press any key to continue]";
	ReadMode('cbreak');
	ReadKey(0);
	ReadMode('normal');
  	}
        elsif($input eq "players" && $tribe eq $name)
        {
        print "the remaining players are:\n\n";
        $~ = "TEAM_TOP";
        write;
        $~ = "TEAM";
        foreach my $member (values(%all))
        {
            $person = $member;
            write;
        }
        $~ = "STDOUT";
	print "\n[press any key to continue]";
	ReadMode('cbreak');
	ReadKey(0);
	ReadMode('normal');
	}
        elsif($input eq "foodora" && $tribe ne $name)
        {
        print "the members of foodora are:\n\n";
        $~ = "TEAM_TOP";
        write;
        $~ = "TEAM";
        foreach my $member (values(%foodora))
        {
            $person = $member;
            write;
        }
        $~ = "STDOUT";
	print "\n[press any key to continue]";
	ReadMode('cbreak');
	ReadKey(0);
	ReadMode('normal');
        }
	system('clear');
        print "who do you vote for? ";
	if($tribe ne $name)
	{
		print "\n[$tribe]";
	}
	else
	{
		print "\n[players]";
	}
	print "\n> ";
        chomp($input = lc(<STDIN>));
    }
    $votes{$input} = 1;
    my %who_voted_who;
    $who_voted_who{$name} = $input;
    foreach $voter (values(%{$tribe_ref}))
    {
        if($voter ne $player)
        {
            my @vote_ar = $voter->getVotes;
            my $vote = @vote_ar[int(rand(@vote_ar-1))];
            while(!$tribe_ref->{$vote}) {$vote = @vote_ar[int(rand(@vote_ar))];}
            if($votes{$vote})
            {
                $votes{$vote}++;
            }
            else
            {
                $votes{$vote} = 1;
            }
            $who_voted_who{$voter->getName} = $vote;
        }
    }
    my $max = 0;
    my $per;
    while((my $keys, my $vals) = each(%votes))
	{
		if($vals > $max)
		{
			$max = $vals;
			$per = $keys;
		}
	}
    system('clear');
    print "the votes are in.";
    ReadMode('cbreak');
	ReadKey(0);
	ReadMode('normal');
    my %inv = $player->gets;
    my $immune = 0;
    if($inv{"hidden immunity idol"})
    {
        system('clear');
        my $input = "uh";
        while($input ne "yes" && $input ne "no")
        {
            print "play hidden immunity idol?\n> ";
            chomp($input = lc(<STDIN>));
            system('clear');
        }
        if($input eq "yes")
        {
            $immune = 1;
	        $player->loses("hidden immunity idol", 1);
            print "$name has played the hidden immunity idol and votes for $name will not count.\n\n[press any key to continue]";
            &generate_location($idol_location);
            &generate_idol_clue;
            ReadMode('cbreak');
            ReadKey(0);
            ReadMode('normal');
        }
    }
    system('clear');
    print "the results are:\n\n";
    $~ = "VOTES_TOP";
    write;
    $~ = "VOTES";
    foreach my $temp (keys(%votes))
    {
        $voter = $temp;
        write;
    }
    $~ = "STDOUT";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    if($immune)
    {
        $max = 0;
        while((my $keys, my $vals) = each(%votes))
        {
            if($vals > $max && $keys ne $player->getName)
            {
                $max = $vals;
                $per = $keys;
            }
        }
    }
    foreach my $playa (values(%{$tribe_ref}))
    {
        if($tribe eq $name)
        {
            if($who_voted_who{$playa->getName} eq $per)
            {
                push(@{$jury{$per}}, $playa->getName);
            }
        }
        $playa->clearVotes;
    }
    if($tribe eq $name)
    {
        @{$jury_alliances{$per}} = &get_allies($per);
    }
    undef %votes;
    delete($all{$per});
    delete($tribe_ref->{$per});
    return $per;
}

sub vote_their_tribe
{
    my $tribe_ref;
    if($_[0] eq "ubungoo")
    {
        $tribe_ref = \%ubungoo;
    }
    elsif($_[0] eq "foodora")
    {
        $tribe_ref = \%foodora;
    }
    else
    {
        $tribe_ref = \%all;
    }
    foreach my $voter (values(%{$tribe_ref}))
    {
        my @vote_ar = $voter->getVotes;
        my $vote = @vote_ar[int(rand(@vote_ar))];
	    while(!$tribe_ref->{$vote}) {$vote = @vote_ar[int(rand(@vote_ar))];}
        if($votes{$vote})
        {
            $votes{$vote}++;
        }
        else
        {
            $votes{$vote} = 1;
        }
    }
    my $max = 0;
    my $per;
    while((my $keys, my $vals) = each(%votes))
	{
		if($vals > $max)
		{
			$max = $vals;
			$per = $keys;
		}
	}
    system('clear');
    print "the results are:\n\n";
    $~ = "VOTES_TOP";
    write;
    $~ = "VOTES";
    foreach my $temp (keys(%votes))
    {
        $voter = $temp;
        write;
    }
    $~ = "STDOUT";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    foreach my $blunt (values(%{$tribe_ref}))
    {
        $blunt->clearVotes;
    }
    undef %votes;
    delete($all{$per});
    delete($tribe_ref->{$per});
    return $per;
}

#############################
##### miscellaneous subs ####
sub contains
{
    my $item = shift;
    my @list = @_;
    my %hash = map { $_ => 1 } @_;
    if($hash{$item})
    {
        return 1;
    }
    return 0;
}

sub remove_duplicates
{
    my %hash = map { $_ => 1 } @_;
    return keys(%hash);
}

sub get_tribe
{
	if($_[0] eq "ubungoo") {return %ubungoo;}
	elsif($_[0] eq "foodora") {return %foodora;}
	else {return %all}
}
sub getNsetTimes
{        
    if($_[0] eq "wit")
    {
        my $return;
	my $fast = 9999999;
        my $hold;
       	if($_[2] == 8)
	{
        	while(my $temp = each(%all))
        	{    
        	    $person = $all{$temp};
        	    $hold = 6 + int(rand(20));
		    if(rand()<.5) {$hold += int(rand(5));}
		    if(rand()<.3) {$hold += int(rand(8));}
		    if(rand()<.2) {$hold += int(rand(10));}
        	    $person->setTime($hold);
		    if($hold < $fast)
        	    {
        	      $fast = $hold;
        	      $return = $person;
        	    }	
	    
        	}
	}
if($_[2] == 9)
	{
        	while(my $temp = each(%all))
        	{    
        	    $person = $all{$temp};
        	    $hold = 15 + int(rand(15));
		    if(rand()<.5) {$hold += int(rand(6));}
		    if(rand()<.3) {$hold += int(rand(8));}
		    if(rand()<.2) {$hold += int(rand(12));}
        	    $person->setTime($hold);
		    if($hold < $fast)
        	    {
        	      $fast = $hold;
        	      $return = $person;
        	    }	
	    
        	}
	}
        if($_[1] <= $fast)
        {
            $player;
        }
        else
        {
            $return;
        }
    }
}
sub getNsetMax
{        
    if($_[0] eq "wit")
    {
        my $return;
	my $most = 0;
        my $hold;
	if($_[2] == 8)
	{
        	while(my $temp = each(%all))
        	{    
        	    $person = $all{$temp};
        	    $hold = 3 + int(rand(7));
		    if(rand()<.5) {$hold += int(rand(1));}
		    if(rand()<.3) {$hold += int(rand(2));}
		    if(rand()<.2) {$hold += int(rand(3));}
        	    $person->setTime($hold);
		    if($hold > $most)
        	    {
        	      $most = $hold;
        	      $return = $person;
        	    }	
	    
        	}
	}
	if($_[2] == 9)
	{
        	while(my $temp = each(%all))
        	{    
        	    $person = $all{$temp};
        	    $hold = 6 + int(rand(11));
		    if(rand()<.5) {$hold += int(rand(1));}
		    if(rand()<.3) {$hold += int(rand(3));}
		    if(rand()<.2) {$hold += int(rand(5));}
        	    $person->setTime($hold);
		    if($hold > $most)
        	    {
        	      $most = $hold;
        	      $return = $person;
        	    }	
	    
        	}
	}
        if($_[1] >= $most)
        {
            $player;
        }
        else
        {
            $return;
        }
    }
}
# adjust player stats
	sub adjust_stats
	{
		while((my $name, my $object) = each(%all))
		{
			if($name ne $player->getName)
			{
			for(my $i = 0; $i < 3; $i++)
			{
				foreach my $thing (keys(%item))
				{
					if(rand()<(1/((1.55)*$item{$thing}) + $object->getStrength()/175000 + $object->getWit()/125000))
					{
						$object->gets($thing, 1);
						if(rand() < .7)
						{
							$object->incStrength(-5);
						}
						elsif(rand() < .8)
						{
							$object->incStrength(-10);
							if(rand() < .05)
							{
								$object->incWit(15);
							}
							elsif(rand() < .3)
							{
								$object->incWit(5);
							}
						}
						elsif(rand()<.05)
						{
							$object->incAlly(-1);
						}
						elsif(rand()<.045)
						{
							$object->incAlly(1);
						}
						else
						{
							$object->incRep(-int(rand(100)));
						}
					}
				}
			}
			}
		}
		while((my $name, my $object) = each(%all))
		{
			if($name ne $player->getName)
			{
			my %has = $object->gets;
			my @pass = %has;
			my @arr = keys(%has);
			my $have = int(rand(2.5));
			my $size = add_vals(@pass);
				while($size > $have)
				{
					if(defined(my $thing = $arr[int(rand(@arr)) - 1]))
					{
						if($thing ne "" && $thing ne "immunity idol" && $thing ne "hidden immunity idol"){
							$object->loses($thing, 1);
							if(rand() < .35)
							{
								$object->incRep($item{$thing});
							}
							else
							{
								$object->incStrength($item{$thing});
							}
						}	
					}
					%has = $object->gets;
					@arr = keys(%has);
					@pass = %has;
					$size = add_vals(@pass);
				}
			}
		}
	}

					sub add_vals
					{
						my %hash = @_;
						my $int = 0;
						while((my $ite, my $val) = each(%hash))
						{
							if(defined($val) && $ite ne "immunity idol" && $ite ne "hidden immunity idol")
							{$int += $val;}
						}
						return $int;
					}
# pass in a tribe and generates votes for each player
sub add_votes
{
    my $tribe_ref;
    if($_[0] eq "ubungoo")
    {
        $tribe_ref = \%ubungoo;
    }
    elsif($_[0] eq "foodora")
    {
        $tribe_ref = \%foodora;
    }
    else
    {
        $tribe_ref = \%all;
    }
    if($_[1] eq "weak")
    {
        my %average_time;
	my $average_strength = 0;
	my $average_wit = 0;
	my $average_rep = 0;
        foreach my $temp (values(%{$tribe_ref}))
        {
            $average_time{$temp->getName} = $temp->getTime;
	    $average_strength += $temp->getStrength;
	    $average_wit += $temp->getWit;
	    $average_rep += $temp->getRep;
        }
        my $count = int(keys(%{$tribe_ref})/2);
   	$average_strength = $average_strength/keys(%{$tribe_ref});
	$average_wit = $average_wit/keys(%{$tribe_ref});
	$average_rep = $average_rep/keys(%{$tribe_ref});
        my @sorted = (sort {$average_time{$b} <=> $average_time{$a} } keys %{$tribe_ref});
        my $start = 1;
        foreach my $temp (@sorted)
        {  
	   for(my $i = 0; $i < $count; $i++)
	   {
           foreach my $plyr (values(%{$tribe_ref}))
           {
		if($temp ne $plyr->getName && !$all{$temp}->isImmune)
		{
			if($tribe_ref->{$temp}->getStrength/$average_strength < (.5 + rand(.75))  && &contains($temp, &get_allies($plyr->getName)) < rand(1.5))
			{
				$plyr->addVote($temp);
			}
			if($tribe_ref->{$temp}->getWit/$average_wit < (.5 + rand(.65))   && &contains($temp, &get_allies($plyr->getName)) < rand(1.5))
			{
				$plyr->addVote($temp);
			}	
			if($tribe_ref->{$temp}->getRep/$average_rep < (.5 + rand(.7))   && &contains($temp, &get_allies($plyr->getName)) < rand(1.5))
			{
				$plyr->addVote($temp);
			}
		}	
			
                if($start == 1)
                {
                    my $sure = 0;
                    while($sorted[$sure] eq $plyr->getName  || $all{$sorted[$sure]}->isImmune) 
                    {$sure++;}
                    $plyr->addVote($sorted[$sure]);
               	}
            }
            $start = 0;
	    $count--;
            }
	}
    }
    elsif($_[1] eq "strong")
    {
        my %average_time;
	my $average_strength = 0;
	my $average_wit = 0;
	my $average_rep = 0;
        foreach my $temp (values(%{$tribe_ref}))
        {
            $average_time{$temp->getName} = $temp->getTime;
	    $average_strength += $temp->getStrength;
	    $average_wit += $temp->getWit;
	    $average_rep += $temp->getRep;
        }
        my $count = int(keys(%{$tribe_ref})/2);
   	$average_strength = $average_strength/keys(%{$tribe_ref});
	$average_wit = $average_wit/keys(%{$tribe_ref});
	$average_rep = $average_rep/keys(%{$tribe_ref});
        my @sorted = (sort {$average_time{$b} <=> $average_time{$a} } keys %{$tribe_ref});
        my $start = 1;
        foreach my $temp (@sorted)
        {  
	   for(my $i = 0; $i < $count; $i++)
	   {
           foreach my $plyr (values(%{$tribe_ref}))
           {
		if($temp ne $plyr->getName && !$all{$temp}->isImmune)
		{
			if($tribe_ref->{$temp}->getStrength/$average_strength > (.5 + rand(.6))  && &contains($temp, &get_allies($plyr->getName)) < rand(1.5))
			{
				$plyr->addVote($temp);
			}
			if($tribe_ref->{$temp}->getWit/$average_wit > (.5 + rand(.75))   && &contains($temp, &get_allies($plyr->getName)) < rand(1.5))
			{
				$plyr->addVote($temp);
			}	
			if($tribe_ref->{$temp}->getRep/$average_rep > (.5 + rand(.65))   && &contains($temp, &get_allies($plyr->getName)) < rand(1.5))
			{
				$plyr->addVote($temp);
			}
		}	
			
                if($start == 1)
                {
                    my $sure = 0;
                    while($sorted[$sure] eq $plyr->getName  || $all{$sorted[$sure]}->isImmune) 
                    {$sure++;}
                    $plyr->addVote($sorted[$sure]);
               	}
            }
            $start = 0;
	    $count--;
            }
    	}
	}
}

sub adjust_rep
{    
    my $average = 0;
    foreach my $temp (values(%all))
    {
        $average += $temp->getTime;
    }
    $average = $average/scalar(keys(%all));
    while((my $name, my $temp) = each(%all))
    {
        $temp->incRep($_[0]*int($average - $temp->getTime));
    }
}
	
#####################################################
################## JURY SELECTOR  ###################

sub jury_vote
{
    system('clear');
    print "the time has come for final tribal council.\n";
    print "\n[press any key to continue]\n";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    print "each jury member will have their turn to speak and at the end will vote.\nthis is the only vote in survivor where you want to see your name.\n";
    print "\n[press any key to continue]\n";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
    my $count = 0;
    my $nice = 0;
    my @votes;
    my $opponent;
    my $input = "fskjdsfsadfasf343aghsdk";
    foreach my $member (keys(%all))
    {
        if($member ne $name)
        {
            $opponent = $member;
        }
    }
    foreach my $member (keys(%jury))
    {
        $votes[$count] = $opponent;
        my $one_greater = $count + 1;
        system('clear');
        print "$positions[$one_greater] to speak is $member.\n";
        print "\n[press any key to continue]\n";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
        system('clear');
        if($count == 0)
        {
            while(!$jury{$input})
            {
                system('clear');
                print "$member wants to know who your least favorite person on the jury is.\n> ";
                chomp($input = lc(<STDIN>));
            }
            if(&contains($name, @{$jury{$input}}) && !&contains($name, @{$jury_alliances{$name}}))
            {
                $votes[$count] = $name;
            }
        }
        elsif($count == 1)
        {
            my $scheme_points = 0;
            my $opponent_points = 0;
            my $truth = 0;
            while($input ne $name && $input ne $opponent)
            {
                system('clear');
                print "$member wants to know who was the bigger schemer between you and $opponent.\n[$name $opponent]\n> ";
                chomp($input = lc(<STDIN>));
            }
            foreach (keys(%jury))
            {
                if(&contains($name, @{$jury{$_}}))
                {
                    $scheme_points++;
                }
                elsif(&contains($opponent, @{$jury{$_}}))
                {
                    $opponent_points++;
                }
            }
            if($scheme_points >= $opponent_points && $input eq $name)
            {
                $truth = 1;
            }
            elsif($scheme_points < $opponent_points && $input eq $opponent)
            {
                $truth = 1;
            }
            if($truth)
            {
                $votes[$count] = $name;
            }
            system('clear');
            print "$member has made their decision.\n";
        }
        elsif($count == 2)
        {
            my $truth = 0;
            while($input ne "yes" && $input ne "no")
            {
                system('clear');
                print "$member wants to know if you've betrayed anyone.\n[yes no]\n> ";
                chomp($input = lc(<STDIN>));
            }
            foreach (keys(%jury))
            {
                if(&contains($name, @{$jury{$_}}) && &contains($name, @{$jury_alliances{$_}}) && $input eq "yes")
                {
                    $truth++;
                }
                if(&contains($name, @{$jury{$_}}) && &contains($name, @{$jury_alliances{$_}}) && $input eq "no")
                {
                    $truth--;
                }
            }
            if($truth < 4 && $truth > -1)
            {
                $votes[$count] = $name;
            }
            system('clear');
            print "$member has made their decision.\n";
        }
        elsif($count == 3)
        {
            system('clear');
            print "$member wants to know your philosophy on life.\n> ";
            <STDIN>;
            if(rand() <= .5)
            {
                $votes[$count] = $name;
            }
            system('clear');
            print "$member has made their decision.\n";
        }
        elsif($count == 4)
        {
            system('clear');
            print "$member is voting off of who has the most allies on the jury.\n";
            my $my_count = 0;
            my $their_count = 0;
            foreach (keys(%jury))
            {
                if(&contains($name, @{$jury_alliances{$_}}))
                {
                    $my_count++;
                }
                elsif(&contains($opponent, @{$jury_alliances{$_}}))
                {
                    $their_count++;
                }
            }
            if($my_count >= $their_count)
            {
                $votes[$count] = $name;
            }
        }
        elsif($count == 5)
        {
            system('clear');
            if(rand() < .5)
            {
                print "$member just hates you.\n";
            }
            else
            {
                $votes[$count] = $name;
                print "$member just hates $opponent.\n";
            }
        }
        elsif($count == 6)
        {
            system('clear');
            print "$member is voting based on who voted for the most people on the jury.\n";
            my $my_count;
            my $their_count;
            foreach (keys(%jury))
            {
                if(&contains($name, @{$jury{$_}}))
                {
                    $my_count++;
                }
                elsif(&contains($opponent, @{$jury{$_}}))
                {
                    $their_count++;
                }
            }
            if($my_count >= $their_count)
            {
                $votes[$count] = $name;
            }
        }
        elsif($count == 7)
        {
            system('clear');
            print "$member is handing you the vote for no reason.\n";
            $votes[$count] = $name;
        }
        elsif($count == 8)
        {
            system('clear');
            print "$member says that you both are awful people, and is just going to flip a coin.\n";
            if(rand() <= .5)
            {
                $votes[$count] = $name;
            }
        }
        $count++;
        print "\n[press any key to continue]\n";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
    }
    system('clear');
    print "the votes are in.";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    my $my_votes = 0;
    my $their_votes = 0;
    my $vote_count = 1;
    foreach (@votes)
    {
        system('clear');
        print "$positions[$vote_count] vote:\n";
        $vote_count++;
        if($_ eq $opponent)
        {
            $their_votes++;
            print "$opponent\n";
        }
        else
        {
            $my_votes++;
            print "$name\n";
        }
        print "\n[press any key to continue]\n";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
    }
    system('clear');
    print "that makes:\n";
    print "$name $my_votes\n";
    print "$opponent $their_votes\n";
    print "\n[press any key to continue]\n";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    my $champion = $opponent;
    if($my_votes >= $their_votes)
    {
        $champion = $name;
    }
    system('clear');
    print "$champion is the sole survivor!!!\n\n";
    goto END;
}

###################
#### formatting ###
sub print_hash
{
	my @arr = &get_allies($person->getName);
    my $temp = "";
    my @ar = $person->gets;
	my @at;
	for(my $k = 0; $k < @ar; $k++){if(defined($ar[$k]) && $ar[$k] ne ""){push(@at, $ar[$k]);}}
	my $max = max(scalar(@at), scalar(@arr));
    for(my $i = 0; $i < $max; $i++)
    {
		my $hold = "";
		my $item = "";
		if(@at > 1)
		{
			my $count = pop(@at);
			$item = pop(@at);
            $temp = $temp . $item . " => " . $count;
			$hold = $item . " => " . $count;
		}
		if(@arr)
		{
			for(my $i = 33 - length($hold); $i > 0; $i--)
			{
				$temp = $temp . " ";
			}
			$temp = $temp . pop(@arr) . "\n";
		}
		else
		{
			$temp = $temp . "\n";
		}
    }
    $temp;
}
sub print_arr
{
    my @arr = $player->gets;
}
###################
#### tutorial #####
sub tutorial
{
	system('clear');
	print "welcome to survivor!\nthis is a game of skill and strategy based off of the tv famous tv show.\n";
	print "this tutorial will explain how the game works and the basics of gameplay\n";
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
	system('clear');
	print "OVERVIEW\n";
	print "======================================================\n";
	print "survivor is a 40 day game consisting of 20 contestants\n";
	print "battling each other to become the sole survivor.      \n";
	print "initially the players are broken into two, ten member \n";
	print "tribes. every other day a single player is elimiated. \n";
	print "after the tenth person is voted off the two tribes    \n";
	print "merge and players continue to get voted off one by one.\n";
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
	system('clear');
	print "TRIBAL COUNCIL\n";
	print "======================================================\n";
	print "at each tribal council, each player anonymously casts \n";
	print "a vote for who they think should leave. the votes are \n";
	print "then tallied and the player with the most votes is    \n";
	print "asked to leave. players with the immunity necklace    \n";
	print "cannot be voted for.				     \n";
    print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
	system('clear');
	print "CHALLENGES\n";
	print "======================================================\n";
	print "before the merge, the two tribes will compete against \n";
	print "each other in immunity challenges. the tribe that     \n";
	print "loses has to go to tribal council the next day.       \n";
	print "after the merge, all the players will compete in the  \n";
	print "challenge for a chance to win the immunity necklace.   \n";
	print "the entire merged tribe then goes to tribal council.  \n";
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
	system('clear');
	print "PLAYER\n";
	print "=======================================================\n";
	print "Strength => gained by eating, lost by hunting, fishing,\n";
	print "            gathering and exploring. strength helps    \n";
	print "            contestants succeed in the challenges      \n";
	print "Wit => wit is gained by exploring, and helps players   \n";
	print "       in challenges as well as helps players convice  \n";
	print "       other players to form an alliance.              \n";
	print "Rep => reputation is gained by doing well in the       \n";
	print "       challenges and represents how the tribe views   \n";
	print "       you. rep helps convince player to ally up       \n";
	print "Ally => this represents your current standing with     \n";
	print "        a player. a higher ally will make it easy to   \n";
	print "        ally up and lessen the chances a player will   \n";
	print "        try to eliminate you.			      \n";
    print "\n[press any key to continue]";
    ReadMode('cbreak');
    ReadKey(0);
    ReadMode('normal');
    system('clear');
	print "TALK";
	print "========================================================\n";
	print "Votes => talking to a player about votes allows you to  \n";
	print "         figure out who they are voting for. after each \n";
	print "         challenge a player will have some idea of who  \n";
	print "         they are voting for. but beware, if you aren't \n";
	print "         in good standing with them they may lie!       \n";
	print "Trash => trash talking another player could hurt their  \n";
	print "         rep but it can also hurt yours!			       \n";
	print "Team Up => ask a player to join an alliance, they will  \n";
	print "           accept based on rep, strength and ally       \n";
	print "Give => give a player an item to boost ally		       \n";
        print "\n[press any key to continue]";
        ReadMode('cbreak');
        ReadKey(0);
        ReadMode('normal');
}

##################################
########## FORMATS ###############
format TEAM_TOP =
name         strength  wit   rep[1-1k]  ally  @<<<<<<<
$skill
============ ========= ===== ========== ===== ========
.
format TEAM =
@<<<<<<<<<<< @<<<<<<<< @<<<< @<<<<<<<<< @<<<< @<<<<<<
$person->getName(), $person->getStrength(), $person->getWit(), $person->getRep(), $person->getAlly(), $person->getTime()
.
format VOTES_TOP =
name         votes
============ =====
.
format VOTES =
@<<<<<<<<<<< @<<<<
$voter, $votes{$voter} 
.

format STDOUT =
name         strength  wit       rep[1-1k] tribe
============ ========= ========= ========= =======
@<<<<<<<<<<< @<<<<<<<< @<<<<<<<< @<<<<<<<< @<<<<<<<<                
$person->getName(), $person->getStrength(), $person->getWit(), $person->getRep(), $person_tribe


inventory                        allies
==============================   ========
@*                               
$invent
.
format MEMORY =
=================== memory ====================
@<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<<
$challenge_words[0],$challenge_words[1],$challenge_words[2],$challenge_words[3]
@<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<<
$challenge_words[4],$challenge_words[5],$challenge_words[6],$challenge_words[7]
@<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<<
$challenge_words[8],$challenge_words[9],$challenge_words[10],$challenge_words[11]
@<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<<
$challenge_words[12],$challenge_words[13],$challenge_words[14],$challenge_words[15]
.
